package com.meitianhui.convenitenbuy.module.store.adapter;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.module.store.model.ShopCarEvent;
import com.meitianhui.convenitenbuy.module.store.model.ShopCarGoods;
import com.meitianhui.convenitenbuy.module.store.model.StoreSelectedGood;
import com.meitianhui.convenitenbuy.util.GlideUtils;
import com.meitianhui.convenitenbuy.util.RxBus;
import com.meitianhui.convenitenbuy.util.TipUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 2017/11/23.
 */

public class StoreSelectGoodAdapter extends BaseQuickAdapter<StoreSelectedGood.ListBean, BaseViewHolder> {
    private HashMap<String, String> mUrlMap;
    private int num = 0;
    private List<ShopCarGoods> shopCarGoodsList = new ArrayList<>();
    List<StoreSelectedGood.ListBean> data;
    public StoreSelectGoodAdapter(@Nullable List<StoreSelectedGood.ListBean> data, HashMap<String, String> urlMap) {
        super(R.layout.item_store_select_good, data);
        this.data = data;
        mUrlMap = urlMap;
        registerEvent();
    }

    @Override
    protected void convert(BaseViewHolder helper, StoreSelectedGood.ListBean item) {
        GlideUtils.setRectImage(mContext,mUrlMap.get(item.getImage_info()), R.mipmap.icon_default_picture_store, R.mipmap.icon_default_picture_store, helper.getView(R.id.iv_good));
        helper.setText(R.id.tv_title, item.getItem_name());
        helper.setText(R.id.tv_price, "¥" + item.getMarket_price());
        if (!TextUtils.isEmpty(item.getSpecification())) {
            helper.setText(R.id.tv_detail, "规格:" + item.getSpecification());
        } else {
            helper.setText(R.id.tv_detail, "规格:");
        }
        String rebate = item.getRebate();
        if (TextUtils.isEmpty(rebate) || rebate.equals("0") || rebate.equals("0.0") || rebate.equals("0.00")) {
            helper.setVisible(R.id.tv_feature_return, false);
        } else {
            helper.setVisible(R.id.tv_feature_return, true);
            helper.setText(R.id.tv_feature_return, "返¥" + rebate + "元");
        }
        helper.setOnClickListener(R.id.tv_add_car, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Integer.parseInt(item.getSale_qty()) <= 0) {
                    TipUtils.showToast(App.getInstance(), "库存不足！");
                    return;
                }

                int[] endXY = new int[2];
                v.getLocationInWindow(endXY);

                ShopCarGoods shopCarGoods;
                boolean isAdd = false;//不存在相同ID
                int listOn = -1;
                for (int i = 0; i < shopCarGoodsList.size(); i++) {
                    if (item.getItem_store_id().equals(shopCarGoodsList.get(i).getItemId())) {
                        isAdd = true;//存在相同ID
                        listOn = i;
                        break;
                    }
                }
                if (!isAdd) {
                    shopCarGoods = new ShopCarGoods();
                    shopCarGoods.setName(item.getItem_name());
                    shopCarGoods.setIschecek(true);
                    shopCarGoods.setNum("1");
                    shopCarGoods.setWeight(item.getWeight());
                    shopCarGoods.setItemId(item.getItem_store_id());
                    shopCarGoods.setImage_info(item.getImage_info());
                    shopCarGoods.setPrice(item.getMarket_price() + "");
                    shopCarGoods.setUrlpic(mUrlMap.get(item.getImage_info()));
                    shopCarGoods.setSpecification(item.getSpecification());
                    shopCarGoods.setStock_qty(item.getSale_qty());
                    shopCarGoods.setRebate(item.getRebate());
                    shopCarGoodsList.add(shopCarGoods);
                    num = num + 1;
                    if (endXY.length == 2) {
                        RxBus.get().post(new ShopCarEvent(Constants.EVENT_TYPE_CAR_ADD, endXY[0], endXY[1], num, shopCarGoodsList));
                    }
                } else {
                    int num_add = (Integer.parseInt(shopCarGoodsList.get(listOn).getNum()) + 1);
                    if (Integer.parseInt(item.getSale_qty()) < num_add) {
                        TipUtils.showToast(App.getInstance(), "库存不足！");
                    } else {
                        num = num + 1;
                        shopCarGoodsList.get(listOn).setNum(num_add + "");
                        if (endXY.length == 2) {
                            RxBus.get().post(new ShopCarEvent(Constants.EVENT_TYPE_CAR_ADD, endXY[0], endXY[1], num, shopCarGoodsList));
                        }
                    }
                }
            }
        });

    }
    public void registerEvent() {
       RxBus.get().toFlowable(ShopCarEvent.class).subscribe(new Consumer<ShopCarEvent>() {
            @Override
            public void accept(ShopCarEvent addShopCarEvent) throws Exception {
                if (addShopCarEvent.type == Constants.EVENT_TYPE_CAR_RETURN) {
                    num =  addShopCarEvent.number;
                    shopCarGoodsList = addShopCarEvent.shopCarGoodsList;
                }

            }
        });
    }

    public void setNewData(List data, HashMap<String, String> map) {
        setNewData(data);
        this.mUrlMap = map;
    }
}
